package com.cmrx.model.util;

import com.cmrx.model.entity.PictureInfo;
import gui.ava.html.image.generator.HtmlImageGenerator;
import net.coobird.thumbnailator.Thumbnails;

import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.*;
import java.util.Base64;
import java.util.HashMap;
import java.util.Map;

public class Html2ImageUtil {

    public static  byte[] createThinPicture(byte[] b){
        byte[] thin = null;
        try{
            String directory = PropertyUtil.getValue("directory");

            String thinPath = directory + UUIDUtil.getUUID()+".png";
            ByteArrayInputStream bin = new ByteArrayInputStream(b);

            OutputStream os = null;
            os = new FileOutputStream(thinPath);
            Thumbnails.of(bin).size(120,120).toOutputStream(os);
            InputStream in = new FileInputStream(thinPath);
            thin = new byte[in.available()];
            in.read(thin);
            bin.close();
            os.close();
            in.close();
            File file = new File(thinPath);
            file.delete();
        }catch (Exception e){
            e.printStackTrace();
        }
        return  thin;
    }


    public static Map<String,Object> htm2Img(String OrginUrlImgBase64, PictureInfo pictureInfo){
        //tubiao1.jpg  tubiao2.jpg
        String directory = PropertyUtil.getValue("directory");
        String newfilepath = directory + UUIDUtil.getUUID()+".jpg";
        String resultFile = directory+UUIDUtil.getUUID()+".png";
        try {
            //base64String转file
            byte[] b = Base64.getDecoder().decode(OrginUrlImgBase64);
            for (int i = 0; i < b.length; ++i) {
                if (b[i] < 0) {// 调整异常数据
                    b[i] += 256;
                }
            }
            File tempFile = new File(newfilepath);
            if (!tempFile.getParentFile().exists()) {
                tempFile.getParentFile().mkdirs();
            }
            OutputStream out = new FileOutputStream(tempFile);
            out.write(b);
            out.flush();
            out.close();
            //生成新图片找到图标等
            String tempFilePath =  tempFile.getAbsolutePath();
            System.out.println("tempFilePath:" + tempFilePath);
            String tubiao1 = tempFilePath.substring(0,tempFilePath.length()-tempFile.getName().length())+"tubiao1.jpg";
            System.out.println("tubiao1:" + tubiao1);
            String tubiao2 = tempFilePath.substring(0,tempFilePath.length()-tempFile.getName().length())+"tubiao2.jpg";
            System.out.println("tubiao2:" + tubiao2);
            //得到html
            String html = Html2ImageUtil.getHtml(pictureInfo,tubiao1,tubiao2,tempFilePath);

            //生成最终图片
            HtmlImageGenerator imageGenerator = new HtmlImageGenerator();
            imageGenerator.loadHtml(html);
            Dimension dimension = new Dimension();
            dimension.setSize(600,690);
            imageGenerator.setSize(dimension);
            BufferedImage bufferedImage =  imageGenerator.getBufferedImage();
            Thread.sleep(1000);
            imageGenerator.saveAsImage(resultFile);
            System.out.println(resultFile);
            //将最终图片变转为base64
            File restfff = new File(resultFile);
            InputStream inputStream = new FileInputStream(restfff);
            byte[] data = new byte[inputStream.available()];
            inputStream.read(data);

            //处理缩略图
            String slpPath = directory + UUIDUtil.getUUID()+".png";
            OutputStream os = null;
            os = new FileOutputStream(slpPath);
            Thumbnails.of(restfff).size(120,120).toOutputStream(os);
            InputStream ins = new FileInputStream(slpPath);
            byte[] suobyte = new byte[ins.available()];
            ins.read(suobyte);
//            System.out.println(Base64.getEncoder().encodeToString(suobyte));

            os.close();
            ins.close();
            inputStream.close();
            File file = new File(slpPath);
            file.delete();

            int width = bufferedImage.getWidth();
            int heigth = bufferedImage.getHeight();
            Map<String,Object> resultMap = new HashMap<>();
            resultMap.put("width",width);
            resultMap.put("heigth",heigth);
            resultMap.put("type","png");
            resultMap.put("content",data);
            resultMap.put("suobyte",suobyte);

            //String res =  Base64.getEncoder().encodeToString(data);
            tempFile.delete();
            restfff.delete();
            return resultMap;
        }catch (Exception e){
            e.printStackTrace();
            return null;
        }
    }

    public static String getHtml(PictureInfo pictureInfo,String tubiao1,String tubiao2,String imgMap){

       /* String html = "<table border='0' width='400' style='font-size: 10px;' align='center'>\n" +
                "\t<tr>\n" +
                "<td colspan='3' align='center' width='400'> \n" +
                "\t现场方位示意图现场方位示意图现场方位示意图现场方位示意图现场方位示意图现场方位示意图现场方位示意图198H奥阿迪6565\n" +
                "</td>\n" +
                "\t</tr>\n" +
                "\t<tr>\n" +
                "<td rowspan='2' align='center' width='200' style='max-height: 400px;'> \n" +
                "\t<img src='file:///"+ imgMap +"' width='200' height='350' align='bottom'/>\n" +
                "</td>\n" +
                "<td align='center' width='200'> \n" +
                "\t<img src='file:///"+ tubiao1 +"' width='150' height='140'/>\n" +
                "</td>\n" +
                "\t</tr>\n" +
                "\t<tr>\n" +
                "<td align='center' width='200'> \n" +
                "\t<table align='center' style='border: 1px solid;border-collapse:collapse' cellspacing='0'>\n" +
                "<tr>\n" +
                "\t<td colspan='2' align='center' style='border: 1px solid;'>图例</td>\n" +
                "</tr>\n" +
                "<tr>\n" +
                "\t<td align='center' style='border: 1px solid;'><img src='file:///"+ tubiao2 +"' width='40' height = '12';/></td>\n" +
                "\t<td align='center' style='border: 1px solid;'>案发地</td>\n" +
                "</tr>\n" +
                "<tr>\n" +
                "\t<td align='center' style='border: 1px solid;'>发案时间</td>\n" +
                "\t<td align='center' style='border: 1px solid;'>"+pictureInfo.getOccurrence_date_from()+"</td>\n" +
                "</tr>\n" +
                "<tr>\n" +
                "\t<td align='center' style='border: 1px solid;'>案发地址</td>\n" +
                "\t<td align='center' style='border: 1px solid;'>"+pictureInfo.getScene_detail()+"</td>\n" +
                "</tr>\n" +
                "<tr>\n" +
                "\t<td align='center' style='border: 1px solid;'>绘制单位</td>\n" +
                "\t<td align='center' style='border: 1px solid;'>"+pictureInfo.getDraw_organize()+"</td>\n" +
                "</tr>\n" +
                "<tr>\n" +
                "\t<td align='center' style='border: 1px solid;'>绘图人</td>\n" +
                "\t<td align='center' style='border: 1px solid;'>"+pictureInfo.getDraw_person()+"</td>\n" +
                "</tr>\n" +
                "<tr>\n" +
                "\t<td align='center' style='border: 1px solid;'>绘图时间</td>\n" +
                "\t<td align='center' style='border: 1px solid;'>"+pictureInfo.getDraw_time()+"</td>\n" +
                "</tr>\n" +
                "\t</table>\n" +
                "</td>\n" +
                "\t</tr>\n" +
                "</table>"; */

        String html = "<table border='0' width='600' style='font-size: 10px;' align='center'>\n" +
                "\t<tr>\n" +
                "<td colspan='3' align='center' width='400' style='font-size: 20px;'> \n" +
                 "<b>"+pictureInfo.getTitle() + "</b>"+
                "</td>\n" +
                "\t</tr>\n" +
                "\t<tr>\n" +
                "<td rowspan='2' align='center' width='350'> \n" +
                "\t<img src='file:///"+ imgMap +"' width='350' height='580' align='bottom'/>\n" +
                "</td>\n" +
                "<td align='center' width='250'> \n" +
                "\t<img src='file:///"+ tubiao1 +"' width='150' height='140'/>\n" +
                "</td>\n" +
                "\t</tr>\n" +
                "\t<tr>\n" +
                "<td align='center' width='250'> \n" +
                "\t<table align='center' style='border: 1px solid;border-collapse:collapse' cellspacing='0'>\n" +
                "<tr>\n" +
                "\t<td colspan='2' align='center' style='border: 1px solid;'>图例</td>\n" +
                "</tr>\n" +
                "<tr>\n" +
                "\t<td align='center' style='border: 1px solid;'><img src='file:///"+ tubiao2 +"' width='40' height = '12';/></td>\n" +
                "\t<td align='center' style='border: 1px solid;'>案发地</td>\n" +
                "</tr>\n" +
                "<tr>\n" +
                "\t<td align='center' style='border: 1px solid;'>发案时间</td>\n" +
                "\t<td align='center' style='border: 1px solid;'>"+pictureInfo.getOccurrence_date_from()+"</td>\n" +
                "</tr>\n" +
                "<tr>\n" +
                "\t<td align='center' style='border: 1px solid;'>案发地址</td>\n" +
                "\t<td align='center' style='border: 1px solid;'>"+pictureInfo.getScene_detail()+"</td>\n" +
                "</tr>\n" +
                "<tr>\n" +
                "\t<td align='center' style='border: 1px solid;'>绘制单位</td>\n" +
                "\t<td align='center' style='border: 1px solid;'>"+pictureInfo.getDraw_organize()+"</td>\n" +
                "</tr>\n" +
                "<tr>\n" +
                "\t<td align='center' style='border: 1px solid;'>绘图人</td>\n" +
                "\t<td align='center' style='border: 1px solid;'>"+pictureInfo.getDraw_person()+"</td>\n" +
                "</tr>\n" +
                "<tr>\n" +
                "\t<td align='center' style='border: 1px solid;'>绘图时间</td>\n" +
                "\t<td align='center' style='border: 1px solid;'>"+pictureInfo.getDraw_time()+"</td>\n" +
                "</tr>\n" +
                "\t</table>\n" +
                "</td>\n" +
                "\t</tr>\n" +
                "</table>";
        return html;
    }



}
